import Uts from "./Uts.js";
/**
 * 过滤器集合
 * @namespace Filter
 */
let Filter = {
    DATE_CHANGE_REG: /-|_|——/g,
    /**
     * formatDate description
     * @param  {object} date   description
     * @param  {string} format description
     * @return {string}        description
     * example
     let date=new Date();
     formatDate(date,'Y-m-d');
     */
    formatDate: function(date, format) {
        if (!Uts.isDate(date) && Uts.isString(date)) {
            date = new Date(date.replace(this.DATE_CHANGE_REG, "/"));
        }
        return date.ZenFormat(format);
    },
    /**
     * cutString
     * @param  {string} value     [description]
     * @param  {number} cutLength [description]
     * @return {string}           [description]
     * @example
     cutstring('some string to cut',8);
     */
    cutString: function(value, cutLength, isShowSybol) {
        let extraString = "";
        if (value && isShowSybol && value.length > cutLength) {
            extraString = "...";
        }
        if (value) {
            return value.substring(0, cutLength) + extraString;
        } else {
            return "";
        }
    },
    /**
     * return fload demical
     * @param  {string} value       [description]
     * @param  {number} floatLength [description]
     * @return {string}             [description]
     * @example
     float(5.232223,2);
     */
    float: function(value, floatLength) {
        if (Uts.isDefined(value)) {
            floatLength = floatLength || 2;
            return parseFloat(value).toFixed(floatLength);
        }
    },
    /**
     * array to string
     * @example
     Uts.ATS([name1,name2,name3],',');
     */
    ATS: function(arr, symbol) {
        if (Uts.isArray(arr)) {
            symbol = symbol || ";";
            return arr.join(symbol);
        }
    },
    /**
     * autoMatch string in array and change
     * @param  {string} text the string to match
     * @param  {object} obj  the object to search
     * @return {string}      the match string result
     * @example 
     autoMatch(5,{"1":待提交,"2":"待审批","3":"预订中","5":"交易已完成"});
     */
    autoMatch: function(text, obj) {
        if (Uts.notEmptyString(text)) {
            return obj[text];
        } else {
            return text;
        }
    },
    /**
     * limit symbol
     * @param {string} string     the string to limit
     * @param {string} symbolList the symbol list to stay
     * @example
     //"wfwx232@@@@@wewf@@abc"
     Filter.LMS('wfwx232@#@@@#@wewf#@@abc','@');
     */
    LMS: function(string, symbolList) {
        if (Uts.notEmptyString(string)) {
            let limitReg = symbolList
                ? new RegExp("[^" + symbolList + "\\w]", "g")
                : /[^\w]/g;
            return string.replace(limitReg, "");
        } else {
            return "";
        }
    }
};

global.Filter = Filter;

export default Filter;
